import Cookies from 'js-cookie';

const app = {
    state: {
        fullscreenLoading: 0,
        sidebar: {
            opened: !Number(Cookies.get('sidebarStatus'))
        },
        language: Cookies.get('language') || 'zh'
    },
    mutations: {
        TOGGLE_SIDEBAR: state => {
            if (state.sidebar.opened) {
                Cookies.set('sidebarStatus', 1);
            } else {
                Cookies.set('sidebarStatus', 0);
            }
            state.sidebar.opened = !state.sidebar.opened;
        },
        SET_LANGUAGE: (state, language) => {
            state.language = language;
            Cookies.set('language', language);
        },
        loading: (state, value) => {
            state.fullscreenLoading += value;
        }
    },
    actions: {
        toggleSideBar({ commit }) {
            commit('TOGGLE_SIDEBAR');
        },
        setLanguage({ commit }, language) {
            commit('SET_LANGUAGE', language);
        },
        loading({ commit }, value) {
            commit('loading', value);
        }
    }
};

export default app;
